package com.cly.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cly.base.contants.CacheKeysConstants;
import com.cly.entity.Document;
import com.cly.mapper.DocumentMapper;
import com.cly.result.JsonRequest;
import com.cly.service.IDocumentService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 文章服务实现类
 *
 * @author by 春风能解释
 * <p>
 * 2025/3/7
 */
@Service
public class DocumentServiceImpl extends ServiceImpl<DocumentMapper, Document>
        implements IDocumentService {

    /**
     * 分页查询文章
     *
     * @param current  页码
     * @param size     页长
     * @param document 查询条件
     * @return JSON
     */
    @Override
    @Cacheable(value = CacheKeysConstants.DocumentKeys.DOCUMENT_LIST, key = "#root.args")
    public JsonRequest<List<Document>> lookDocument(Integer current, Integer size, Document document) {
        IPage<Document> page = baseMapper.lookDocument(new Page<>(current, size), document);
        return JsonRequest.success(page.getRecords(), page.getTotal());
    }

    /**
     * 查询文章详情
     *
     * @param id 文章id
     * @return JSON
     */
    @Override
    @Cacheable(value = CacheKeysConstants.DocumentKeys.DOCUMENT_LIST, key = "#id")
    public JsonRequest<Document> lookOneDocument(Long id) {
        return null;
    }

    /**
     * 添加文章
     *
     * @param document 文章
     * @return JSON
     */
    @Override
    @CacheEvict(value = CacheKeysConstants.DocumentKeys.DOCUMENT_LIST, allEntries = true)
    public JsonRequest<Integer> addOneDocument(Document document) {
        return JsonRequest.success(baseMapper.insert(document));
    }

    /**
     * 修改文章
     *
     * @param document 文章
     * @return JSON
     */
    @Override
    @CacheEvict(value = CacheKeysConstants.DocumentKeys.DOCUMENT_LIST, allEntries = true)
    public JsonRequest<Integer> updateOneDocument(Document document) {
        return JsonRequest.success(baseMapper.updateById(document));
    }

    /**
     * 删除文章
     *
     * @param id 文章id
     * @return JSON
     */
    @Override
    @CacheEvict(value = CacheKeysConstants.DocumentKeys.DOCUMENT_LIST, allEntries = true)
    public JsonRequest<Integer> deleteDocument(List<Long> id) {
        return JsonRequest.success(baseMapper.deleteByIds(id));
    }
}




